package vn.cuhanh.utils;

import vn.cuhanh.sys.DBAdapter;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public final class SystemLog {
	private DBAdapter db;
	private Context ctx;
	private long transactionId = 0;
	private String table = "log";

	public SystemLog(Context context) {
		DBAdapter db = new DBAdapter(context, this.table);
		this.setCtx(context);
		this.setDb(db);
	}

	public SystemLog(Context context, String tableName) {
		DBAdapter db = new DBAdapter(context, tableName);
		this.setCtx(context);
		this.setDb(db);
	}

	public SystemLog(DBAdapter db) {
		this.setDb(db);
	}

	public long log(ContentValues values) {
		long count = 0;
		try {
			this.db.open();
			count = this.db.insert(values);

		} catch (Exception e) {
			HelperFunc.readStackTrace(e);
			this.db.close();
		} finally {
			this.db.close();
		}
		return count;
	}

	public Cursor getAllLog() {
		Cursor cur = null;
		try {
			this.db.open();
			cur = this.db.fetchAll();
		} catch (Exception e) {
			HelperFunc.readStackTrace(e);
			this.db.close();
		} finally {
			this.db.close();
		}
		return cur;
	}

	public void deleteAllLog() {
		try {
			this.db.open();
			this.db.deleteAll();
		} catch (Exception e) {
			HelperFunc.readStackTrace(e);
			this.db.close();
		} finally {
			this.db.close();
		}
	}

	public DBAdapter getDb() {
		return db;
	}

	public void setDb(DBAdapter db) {
		this.db = db;
	}

	public long getTransactionId() {
		return transactionId;
	}

	public void setTransactionId(long transaction_id) {
		this.transactionId = transaction_id;
	}

	public Context getCtx() {
		return ctx;
	}

	public void setCtx(Context ctx) {
		this.ctx = ctx;
	}

}
